package com.zeny.coding.controller.admin.file;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.AppendObjectRequest;
import com.aliyun.oss.model.AppendObjectResult;
import com.aliyun.oss.model.ObjectMetadata;

import com.zeny.coding.config.OssConfig;
import com.zeny.coding.dto.FileDto;
import com.zeny.coding.dto.Response;
import com.zeny.coding.enmus.FileUseEnum;
import com.zeny.coding.service.FileService;
import com.zeny.coding.util.Base64ToMultipartFile;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;

/**
 * <h2>阿里云oss服务</h2>
 **/
@Slf4j
@RestController
@RequestMapping("/file/admin")
@SuppressWarnings("all")
public class OssController {

    @Autowired
    private OssConfig ossConfig;

    @Autowired
    private FileService fileService;

    /**
     * <h2>阿里云oss-追加上传</h2>
     * @param fileDto
     **/
    @PostMapping("/oss-append")
    public Response fileUpload(@RequestBody FileDto fileDto) throws Exception {
        fileDto.valid();
        log.info("上传文件开始");
        String use = fileDto.getUse();//文件用途
        String key = fileDto.getKey(); //文件唯一标识key
        String suffix = fileDto.getSuffix(); //文件后缀
        Integer shardIndex = fileDto.getShardIndex(); //文件分片索引
        Integer shardSize = fileDto.getShardSize();   //文件分片大小|B
        String shardBase64 = fileDto.getShard();      //文件的base64编码字符串
        MultipartFile shard = Base64ToMultipartFile.base64ToMultipart(shardBase64); //base64字符串转文件
        FileUseEnum useEnum = FileUseEnum.getByCode(use); //文件的用途枚举 教师 |课程
        String dir = useEnum.name().toLowerCase(); //文件目录
        String path = new StringBuffer(dir)  //文件相对路径: 目录/key.suffix
                .append("/")
                .append(key)
                .append(".")
                .append(suffix)
                .toString();
        OSS ossClient = new OSSClientBuilder()
                .build(ossConfig.getEndpoint(),
                        ossConfig.getAccessKeyId(),
                        ossConfig.getAccessKeySecret());
        ObjectMetadata meta = new ObjectMetadata();
        meta.setContentType("text/plain");// 指定上传的内容类型。
        AppendObjectRequest appendObjectRequest = new AppendObjectRequest(
                ossConfig.getBucket(), path, new ByteArrayInputStream(shard.getBytes()), meta);
        //设置文件追加位置
        appendObjectRequest.setPosition((long) ((shardIndex - 1) * shardSize));
        AppendObjectResult appendObjectResult = ossClient.appendObject(appendObjectRequest);
        System.out.println(appendObjectResult.getObjectCRC());
        System.out.println(JSONObject.toJSONString(appendObjectResult));
        ossClient.shutdown();
        log.info("保存文件记录开始");
        fileDto.setPath(path);
        fileService.save(fileDto);
        fileDto.setPath(ossConfig.getDomain() + path);
        return new Response().success().data(fileDto);
    }


}
